#include "ui_mainwindow.h"
#include <qtextedit.h>
#include <mainwindow.h>
#include <WorkerThread.h>
#include <QCoreApplication>
#include <QKeyEvent>
#include <QFileDialog>

MainWindow* MainWindow::instance = NULL;


MainWindow* MainWindow::getInstance() {
    if (instance == NULL)
        instance= new MainWindow();
    return instance;
}

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
    ui->setupUi(this);

    QPushButton *ejecutarButton = this->findChild<QPushButton*>("ejecutarButton");
    QPushButton *abrirButton = this->findChild<QPushButton*>("abrirButton");
    QComboBox *operacionComboBox = this->findChild<QComboBox*>("operacionComboBox");


    QObject::connect(abrirButton, SIGNAL(clicked()), this, SLOT(getFilePath()));
    QObject::connect(ejecutarButton, SIGNAL(clicked()), this, SLOT(ejecutarComando()));
    QObject::connect(operacionComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(prepararOperacion(int)));
    QObject::connect(this, SIGNAL(updateSalidaSignal(QString)), this, SLOT(updateSalida(QString)));
    operacionComboBox->setCurrentIndex(1);
}


void MainWindow::ejecutarComando(){


    QPushButton *ejecutarButton = this->findChild<QPushButton*>("ejecutarButton");
    ejecutarButton->setEnabled(false);

    QComboBox *operacionComboBox = this->findChild<QComboBox*>("operacionComboBox");
    QLineEdit *operacionEditText = this->findChild<QLineEdit*>("operacionLineEdit");
    QLineEdit *indiceEditText = this->findChild<QLineEdit*>("indiceLineEdit");

    QTextEdit *salidaEditText= this->findChild<QTextEdit*>("salidaEditText");
    salidaEditText->clear();

    nombreIndice=indiceEditText->displayText().toStdString();

    int operation=operacionComboBox->currentIndex();

    if(operation==0){
        isConsulta=false;
        repositorio=operacionEditText->displayText().toStdString();
        if(repositorio.empty())
            writeToStdOuT("Debe completar el campo repositorio");
    }else{
        isConsulta=true;
        consulta=operacionEditText->displayText().toStdString();
        if(consulta.empty()){
            writeToStdOuT("Debe completar el campo frase");
            ejecutarButton->setEnabled(true);
            return ;
        }
    }

    if(nombreIndice.empty()){
        writeToStdOuT("Debe completar el campo indice");
        ejecutarButton->setEnabled(true);
        return;
    }

    WorkerThread *workerThread = new WorkerThread();
    connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
    connect(workerThread, SIGNAL(resultReady()), workerThread, SLOT(onFinished()));
    workerThread->start();

}


bool MainWindow::event(QEvent *event){
    if (event->type() == QEvent::KeyPress){
        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
        if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return){
            QPushButton *ejecutarButton = this->findChild<QPushButton*>("ejecutarButton");
            ejecutarButton->animateClick();
            return true;
        }
    }
    return QMainWindow::event(event);
}

void MainWindow::prepararOperacion(int operation){

    QPushButton *abrirButton = this->findChild<QPushButton*>("abrirButton");
    QLabel *operacionTitle = this->findChild<QLabel*>("operacionTitle");
    QLineEdit *operacionEditText = this->findChild<QLineEdit*>("operacionLineEdit");
    QString qstr;

    operacionEditText->setText("");
    if(operation==0){
        qstr = QString::fromStdString("Repositorio");
        abrirButton->setVisible(true);
        operacionEditText->setReadOnly(true);
    }else{
        qstr = QString::fromStdString("Frase");
        operacionEditText->setReadOnly(false);
        abrirButton->setVisible(false);
    }


    operacionTitle->setText(qstr);
}

void MainWindow::writeToStdOuT(string mje){
    QString qstr = QString::fromStdString(mje);
    emit updateSalidaSignal(qstr);
}

void MainWindow::updateSalida(const QString &s){
    QTextEdit* salidaText = this->findChild<QTextEdit*>("salidaEditText");
    salidaText->append(s);
}

void MainWindow::getFilePath(){
    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"",QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    QLineEdit *operacionEditText = this->findChild<QLineEdit*>("operacionLineEdit");
    operacionEditText->setText(dir);
}

string MainWindow::getNombreIndice(){
    return nombreIndice;
}

string MainWindow::getRepositorio(){
    return repositorio;

}

string MainWindow::getConsulta(){
    return consulta;
}

void MainWindow::cleanInputVariables(){
    consulta.clear();
    repositorio.clear();
    nombreIndice.clear();
}

bool MainWindow::isOPConsulta(){
    return isConsulta;
}


MainWindow::~MainWindow(){
    delete ui;
}

